<?php
	
	// Add RSS links to <head> section
	automatic_feed_links();
	
	
	// Clean up the <head>
	function removeHeadLinks() {
    	remove_action('wp_head', 'rsd_link');
    	remove_action('wp_head', 'wlwmanifest_link');
    }
    add_action('init', 'removeHeadLinks');
    remove_action('wp_head', 'wp_generator');
    
	// Declare sidebar widget zone
    if (function_exists('register_sidebar')) {
    	register_sidebar(array(
    		'name' => 'Sidebar Widgets',
    		'id'   => 'sidebar-widgets',
    		'description'   => 'These are widgets for the sidebar.',
    		'before_widget' => '<div id="%1$s" class="widget %2$s">',
    		'after_widget'  => '</div>',
    		'before_title'  => '<h2>',
    		'after_title'   => '</h2>'
    	));
    }
    
    if (function_exists('register_nav_menus')) {
		register_nav_menus(
			array(
				'main_nav' => 'Main Navigation Menu',
				'footer_nav' => 'Footer Navigation Menu'
			)
		);
	}
	

function setbodyclass($classes){
$classes_new = array();
	if(is_front_page()){
		$classes[] = 'index';
		$classes_new = $classes;
	}else{
		foreach($classes as $cl){
			if($cl != 'index') $classes_new[] = $cl;
		}
	}
	return $classes_new;
}
add_filter('body_class', 'setbodyclass');		
function twentytwelve_setup() {
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 624, 9999 ); // Unlimited height, soft crop
}
add_action( 'after_setup_theme', 'twentytwelve_setup' );	
include_once(TEMPLATEPATH . '/inc/building/load.php');

$themename = 'Template';
$shortname = 'custom';
$options = array (

	array(	"name" => "Support",
			"type" => "title"),
			
	array(	"type" => "open"),
	
	array(	"name" => "Name of people support",
				"desc" => "Name of people support",
				"id" => $shortname."_name_support",
				"type" => "text"),
	array(	"name" => "Phone Header",
				"desc" => "Phone nummber",
				"id" => $shortname."_phone_support",
				"type" => "text"),			
	array(	"name" => "Email Support",
			"desc" => "Enter text in...",
			"id" => $shortname."_email_support",
			"type" => "text"),	
	array(	"name" => "Hotline",
				"desc" => "Phone nummber",
				"id" => $shortname."_hotline_support",
				"type" => "text"),		
	array(	"name" => "Yahoo Address",
				"desc" => "Yahoo Address",
				"id" => $shortname."_yahoo_support",
				"type" => "text"),	
	array(	"name" => "Name display",
				"desc" => "Name display for yahoo address",
				"id" => $shortname."_yahoo_name",
				"type" => "text"),				
	array(	"name" => "Skype Address",
				"desc" => "Skype Addressr",
				"id" => $shortname."_skype_support",
				"type" => "text"),	
	array(	"name" => "Name display",
				"desc" => "Name display for skype address",
				"id" => $shortname."_skype_name",
				"type" => "text"),					
	array(	"name" => "Facebook Address",
				"desc" => "Link to facebook",
				"id" => $shortname."_face_support",
				"type" => "text"),			
	array(	"type" => "close")
);

function mytheme_add_admin() {
    global $themename, $shortname, $options;
    if ( $_GET['page'] == basename(__FILE__) ) {
    
        if ( 'save' == $_REQUEST['action'] ) {
                foreach ($options as $value) {
                    update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
                foreach ($options as $value) {
                    if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }
                header("Location: themes.php?page=functions.php&saved=true");
                die;
        } else if( 'reset' == $_REQUEST['action'] ) {
            foreach ($options as $value) {
                delete_option( $value['id'] ); }
            header("Location: themes.php?page=functions.php&reset=true");
            die;
        }
    }
    add_theme_page($themename." Options", "".$themename." Options", 'edit_theme_options', basename(__FILE__), 'mytheme_admin');
}

function mytheme_admin() {

    global $themename, $shortname, $options;

    if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
    if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';
    
?>
<div class="wrap">
	<h2><?php echo $themename; ?> settings</h2>

<form method="post">
<?php foreach ($options as $value) { 
    
	switch ( $value['type'] ) {
	
		case "open":
		?>
			<table width="100%" class="form-table" >        
			<?php break;		
		case "close":
		?>		
			</table><br />
		<?php break;
		
		case "title":
		?>
		<table width="100%">
			<tr>
				<td colspan="2" class="aaa"><h3><?php echo $value['name']; ?></h3></td>
			</tr>                
        
		<?php break;

		case 'text':
		?>
        
        <tr valign="top">
            <th scope="row"><?php echo $value['name']; ?></th>
			<td>
				<input 
					id="<?php echo $value['id']; ?>"
					name="<?php echo $value['id']; ?>"
					class="regular-text"
					type="<?php echo $value['type']; ?>"
					placeholder="<?php echo $value['desc']; ?>"
					value="<?php if ( get_settings( $value['id'] ) != "") { echo stripslashes (get_settings( $value['id'] )); } else { echo stripslashes ($value['std']); } ?>"
				/>
			</td>
            
        </tr>

		<?php 
		break;
		
		case 'textarea':
		?>
		<tr valign="top">
            <th scope="row"><?php echo $value['name']; ?></th>
			<td>
				<textarea 
					id="<?php echo $value['id']; ?>"
					name="<?php echo $value['id']; ?>"		
					class="large-text"
					placeholder="<?php echo $value['desc']; ?>"
				><?php if ( get_settings( $value['id'] ) != "") { echo stripslashes (get_settings( $value['id'] )); } else { echo stripslashes ($value['std']); } ?></textarea>
			</td>
            
        </tr>
        
		<?php 
		break;
		
		case 'select':
		?>
		<tr valign="top">
            <th scope="row"><?php echo $value['name']; ?></th>
			<td>
				<select
					id="<?php echo $value['id']; ?>"
					name="<?php echo $value['id']; ?>"
				>
				<?php foreach ($value['options'] as $option) { ?><option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?>
				</select>
				<small><?php echo $value['desc']; ?></small>
			</td>
            
        </tr>
		
		<?php
        break;
            
		case "checkbox":
		?>
		<tr valign="top">
            <th scope="row"><?php echo $value['name']; ?></th>
			<td>
				<? if(get_settings($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = ""; } ?>
				<input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
				<?php echo $value['desc']; ?>
			</td>
        </tr>
        <?php 		break;
	} 
}
?>
	<p class="submit">
		<input name="save" class="button-primary" type="submit" value="Save changes" />    
		<input type="hidden" name="action" value="save" />
	</p>
</form>
<form method="post">
	<p class="submit">
		<input name="reset" class="button-primary" type="submit" value="Reset" />
		<input type="hidden" name="action" value="reset" />
	</p>
</form>
<?php
}
add_action('admin_menu', 'mytheme_add_admin'); 

function checkActiveMenuHome($id){
global $post;
$classes = '';
if(is_archive() && get_post_type( $post->ID ) == 'building'){
	$queried_object = get_queried_object();
	if($queried_object->parent == $id || $queried_object->term_id == $id)
		$classes = ' class="active"';
}
return $classes;	
}
?>